package comu.hnevc.zh.testdemo.fragment;

import android.support.v4.app.Fragment;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Timer;
import java.util.TimerTask;

import comu.hnevc.zh.testdemo.R;
import comu.hnevc.zh.testdemo.base.BaseUrl;

public class RoadconditionFragment extends Fragment {
    private ImageView imageView;
    private Bitmap bitmap;
    private Canvas canvas;
    //严重拥堵
    private Paint yzp;
    //中度拥堵
    private Paint zdp;
    //一般拥堵
    private Paint ybp;
    // 缓行
    private Paint hxp;
    // 畅通
    private Paint ctp;
    // 文字颜色
    private Paint txtp;
    private Timer timer;
    private RequestQueue mQueue;
    private TimerTask task;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.roadcondition_fragment,container,false);

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        imageView=getActivity().findViewById(R.id.iv_roadcondition);
        bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.guide_1).copy(Bitmap.Config.ARGB_8888,true);
        //mutable可改变的，将返回一个可变的位图。这可以用于例如以编程方式将效果应用于通过BitmapFactory加载的位图。这个参数为true时，才能对像素位进行修改。
        canvas=new Canvas(bitmap);
        txtp=new Paint();
        txtp.setColor(getResources().getColor(R.color.blank));
      txtp.setTextSize(50);
      yzp=new Paint();
      yzp.setColor(getResources().getColor(R.color.yzyd));
      zdp=new Paint();
      zdp.setColor(getResources().getColor(R.color.zdyd));
      ybp=new Paint();
      ybp.setColor(getResources().getColor(R.color.yzyd));
      hxp=new Paint();
      hxp.setColor(getResources().getColor(R.color.hx));
      ctp=new Paint();
      ctp.setColor(getResources().getColor(R.color.ct));
      timer =new Timer();
        task=new TimerTask() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void run() {
                initdata();
            }
        };
        timer.schedule(task,0,15000);

    }


    @Override
    public void onDestroy() {
        task.cancel();
        super.onDestroy();

    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    private void initdata() {
        SharedPreferences spf=getActivity().getSharedPreferences("data",Context.MODE_PRIVATE);
        String username=spf.getString("UserName","user2");
        mQueue=Volley.newRequestQueue(getContext());
        for (int i=0;i<7;i++){
            final JSONObject object=new JSONObject();

            try {
                object.put("RoadId",i+1);
                object.put("UserName",username);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            JsonObjectRequest jor=new JsonObjectRequest(Request.Method.POST, BaseUrl.BASEURL + BaseUrl.GetRoadStatus, object, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject jsonObject) {
                    try {
                        int status=jsonObject.getInt("Status");
                        initview(object.getInt("RoadId"),status);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }


                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {

                }
            });
            mQueue.add(jor);
        }

    }

    private void initview(int roadId, int status) {
        if (roadId == 1) {
            if (status==1){
                canvas.drawRect(300,120,350,500,ctp);
            }else if (status==2){
                canvas.drawRect(300,120,350,500,hxp);
            }else if (status==3){
                canvas.drawRect(300,120,350,500,ybp);
            }else if (status==4){
                canvas.drawRect(300,120,350,500,zdp);
            }else if (status==5){
                canvas.drawRect(300,120,350,500,yzp);
            }
            canvas.drawText("学",300,200,txtp);
            canvas.drawText("院",300,300,txtp);
            canvas.drawText("路",300,400,txtp);
        }else if (roadId==2){
            if (status==1){
                canvas.drawRect(540,120,610,500,ctp);
            }else if (status==2){
                canvas.drawRect(540,120,610,500,hxp);
            }else if (status==3){
                canvas.drawRect(540,120,610,500,ybp);
            }else if (status==4){
                canvas.drawRect(540,120,610,500,zdp);
            }else if (status==5){
              canvas.drawRect(540,120,610,500,yzp);
            }
            canvas.drawText("联",540,300,txtp);
            canvas.drawText("想",540,300,txtp);
            canvas.drawText("路",540,500,txtp);
        }else if (roadId==3){
            if (status==1){
                canvas.drawRect(540,600,610,950,ctp);
            }else if (status==2){
                canvas.drawRect(540,600,610,950,hxp);
            }else if (status==3){
                canvas.drawRect(540,600,610,950,ybp);
            }else if (status==4){
                canvas.drawRect(540,600,610,950,zdp);
            }else if (status==5){
                canvas.drawRect(540,600,610,950,yzp);
            }
            canvas.drawText("医",540,700,txtp);
            canvas.drawText("院",540,700,txtp);
            canvas.drawText("路",540,700,txtp);

        }else if (roadId==4){
            if (status==1){
                canvas.drawRect(300,600,360,950,ctp);
            }else if (status==2){
                canvas.drawRect(300,600,360,950,hxp);
            }else if (status==3){
                canvas.drawRect(300,600,360,950,ybp);
            }else if (status==4){
                canvas.drawRect(300,600,360,950,zdp);
            }else if (status==5){
                canvas.drawRect(300,600,360,950,yzp);
            }
            canvas.drawText("幸",300,700,txtp);
            canvas.drawText("福",300,800,txtp);
            canvas.drawText("路",300,900,txtp);
        }else if (roadId==5){
            if (status==1){
                canvas.drawRect(40,950,1100,1040,ctp);
                canvas.drawRect(40,40,110,950,ctp);
                canvas.drawRect(110,40,1100,120,ctp);
            }else if (status==2){
                canvas.drawRect(40,950,1100,1040,hxp);
                canvas.drawRect(40,40,110,950,hxp);
                canvas.drawRect(110,40,1100,120,hxp);
            }else if (status==3){
                canvas.drawRect(40,950,1100,1040,ybp);
                canvas.drawRect(40,40,110,950,ybp);
                canvas.drawRect(110,40,1100,120,ybp);
            }else if (status==4){
                canvas.drawRect(40,950,1100,1040,zdp);
                canvas.drawRect(40,40,110,950,zdp);
                canvas.drawRect(110,40,1100,120,zdp);
            }else if (status==5){
                canvas.drawRect(40,950,1100,1040,yzp);
                canvas.drawRect(40,40,110,950,yzp);
                canvas.drawRect(110,40,1100,120,yzp);
            }
            canvas.drawText("环",500,950,txtp);
            canvas.drawText("城",600,950,txtp);
            canvas.drawText("快",700,950,txtp);
            canvas.drawText("速",800,950,txtp);
            canvas.drawText("路",900,950,txtp);

            canvas.drawText("环",500,100,txtp);
            canvas.drawText("城",600,100,txtp);
            canvas.drawText("快",700,100,txtp);
            canvas.drawText("速",800,100,txtp);
            canvas.drawText("路",900,100,txtp);

            canvas.drawText("环",50,300,txtp);
            canvas.drawText("城",50,400,txtp);
            canvas.drawText("快",50,500,txtp);
            canvas.drawText("速",50,600,txtp);
            canvas.drawText("路",500,700,txtp);

        }else if (roadId==6){
            if (status==1){
                canvas.drawRect(1040,120,1100,950,ctp);
            }else if (status==2){
                canvas.drawRect(1040,120,1100,950,hxp);
            }else if (status==3){
                canvas.drawRect(1040,120,1100,950,ybp);
            }else if (status==4){
             canvas.drawRect(1040,120,1100,950,zdp);
            }else if (status==5){
                canvas.drawRect(1040,120,1100,950,yzp);
            }
            canvas.drawText("环",1040,300,txtp);
            canvas.drawText("城",1040,400,txtp);
            canvas.drawText("高",1040,500,txtp);
            canvas.drawText("速",1040,600,txtp);
        }else if (roadId==7){
            if (status==1){
                canvas.drawRect(750,410,900,660,ctp);
            }else if (status==2){
              canvas.drawRect(750,410,900,660,hxp);
            }else if (status==3){
                canvas.drawRect(750,410,900,660,ybp);
            }else if (status==4){
                canvas.drawRect(750,410,900,660,zdp);
            }else if (status==5){
             canvas.drawRect(750,410,900,660,yzp);
            }
            canvas.drawText("停",750,500,txtp);
            canvas.drawText("车",750,550,txtp);
            canvas.drawText("场",750,600,txtp);
        }
        imageView.setImageBitmap(bitmap);


        }
    }

